/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.facade.enums;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.enums.Messageable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>(BatteryType)</b>
 * 电池类型,
 * 01H:铅酸电池;
 * 02H:氢电池;
 * 03H:磷酸铁锂电池;
 * 04H:锰酸锂电池;
 * 05H:钴酸锂电池;
 * 06H:三元材料电池;
 * 07H:聚合物锂离子电池;
 * 08H:钛酸锂电池;
 * FFH:其他;
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/7/2
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum DianlanBatteryType implements Messageable {
    /** lead-acid battery */
    Lead_Acid("01H", "铅酸电池"),

    /** nickel-metal hydride battery */
    Ni_MH("02H", "镍氢电池"),

    /** Lithium iron phosphate battery */
    Lfp("03H", "磷酸铁锂电池"),

    /** Lithium manganate battery */
    Lithium_Manganate("04H", "锰酸锂电池"),

    /** Lithium cobalt acid battery */
    Lithium_Cobalt_Acid("05H", "钴酸锂电池"),

    /** Ternary lithium battery */
    MnNiCo("06H", "三元锂电池"),

    /** polymer li-ion battery */
    Polymer_Li_ion("07H", "聚合物锂离子电池"),

    /** Lithium titanate battery */
    Lithium_Titanate("08H", "钛酸锂电池"),

    /** 不晓得啷格回事 */
    UNKNOWN("--", "不晓得啷格回事");

    /** 枚举值 */
    String code;

    /** 枚举描述 */
    String message;


    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @param code 枚举值
     *
     * @return BatteryType
     */
    public static DianlanBatteryType getByCode(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 通过枚举<code>name</code>获得枚举
     *
     * @param name 枚举名
     *
     * @return BatteryType
     */
    public static DianlanBatteryType getByName(String name) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equalsIgnoreCase(name, innerEnum.name()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 获取全部枚举
     *
     * @return List<BatteryType>
     */
    public static List<DianlanBatteryType> getAllEnum() {
        return Lists.newArrayList(values());
    }

    /**
     * 获取全部枚举值
     *
     * @return List<String>
     */
    public static List<String> getAllEnumCode() {
        return getAllEnum().stream().map(DianlanBatteryType::code).collect(Collectors.toList());
    }

    /**
     * 通过code获取msg
     *
     * @param code 枚举值
     *
     * @return String
     */
    public static String getMsgByCode(String code) {
        return getByCode(code).message();
    }

    /**
     * 获取枚举code
     *
     * @param innerEnum 枚举
     *
     * @return code
     */
    public static String getCode(DianlanBatteryType innerEnum) {
        return Optional.ofNullable(innerEnum).map(DianlanBatteryType::code).orElse(UNKNOWN.code());
    }

    /**
     * map
     *
     * @return Map
     */
    public static Map<String, String> maps() {
        return Stream.of(values()).collect(Collectors.toMap(DianlanBatteryType::code, DianlanBatteryType::message));
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值
     */
    public static <T> T safeGet(DianlanBatteryType enumValue, Function<DianlanBatteryType, T> mapper, T defaultValue) {
        return Objects.isNull(enumValue) ? defaultValue : mapper.apply(enumValue);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值，默认返回值是null
     */
    public static <T> T safeGet(DianlanBatteryType enumValue, Function<DianlanBatteryType, T> mapper) {
        return safeGet(enumValue, mapper, null);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的code值，默认返回值是null
     */
    public static String safeGetCode(DianlanBatteryType enumValue) {
        return safeGet(enumValue, Messageable::code, null);
    }

    /**
     * 判断给定的枚举，是否在列表中
     *
     * @param values 列表
     *
     * @return Boolean
     */
    public boolean isInList(DianlanBatteryType... values) {
        for (DianlanBatteryType e : values) {
            if (this == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断给定的枚举，是否不在列表中
     *
     * @param values 列表
     *
     * @return Boolean
     */
    public boolean isNotInList(DianlanBatteryType... values) {
        List<DianlanBatteryType> statusList = Lists.newArrayList(values);
        return !CollUtil.contains(statusList, this);
    }

    /**
     * @return Returns the code.
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    @Override
    public String message() {
        return message;
    }
}